package cc.xsubj.trading.messages;

import java.util.LinkedList;
import java.util.List;

import cc.xsubj.engine.Globals;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.geom.Rectangle;



public class TextOnlyMessage implements Message
{
    private String text;
    private String name;
    private static TrueTypeFont textFont = new TrueTypeFont(new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 10), false);
    private static TrueTypeFont nameFont = new TrueTypeFont(new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 9), false);
//    private static Font font = ResourceManager.getFont("fixed_font");
    /* Old stuff
    private static UnicodeFont font = new UnicodeFont(new java.awt.Font("fonts/fixed.ttf", java.awt.Font.PLAIN, 12));
    private static TrueTypeFont font;
    private static TrueTypeFont font = new TrueTypeFont(new java.awt.Font("c:/windows/fonts/arial.ttf",java.awt.Font.PLAIN, 12), false);
     * 
     */
    private static int width = 150;
    private static int paddingX = 4;
    private static int paddingY = -3;
    
    protected static int lineWidth = width - paddingX*2;
    private static int linesPerMessage = 3;
    private static int height = 40;
    private static Image textImage;
    private static Graphics textGraphics;
    private static Rectangle messageBox;
    
    private Image nameImage;
    private Graphics nameGraphics;
    
    static {
        try {
			textImage = new Image(width, height);
			textGraphics = textImage.getGraphics();
		} catch (SlickException e) {
			e.printStackTrace();
		}
    }

    public TextOnlyMessage(String text)
    {
    	this(null, text);
    }
    
    public TextOnlyMessage(String name, String text)
    {
        this.name = name;
        this.text = text;
        
        
        try
        {
        	if(name != null)
        	{
        		nameImage = new Image(nameFont.getWidth(name)+paddingX*2, nameFont.getHeight(name));
        		nameGraphics = nameImage.getGraphics();
        	}
        	
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    @Override
    public void resolve()
    {
        System.out.println(text);
    }

    public static List<String> splitText(String text)
    {
        List<String> lines = new LinkedList<String>();
        StringBuilder currentLine = new StringBuilder();
        int widthLeft = lineWidth;
        int lineNumber = 1;
        for (String word : text.split(" "))
        {
            int wordWidth = textFont.getWidth(word+" ");
            if (wordWidth > widthLeft)
            {
                currentLine.append("\n");
                if (lineNumber % linesPerMessage == 0)
                {
                    lines.add(currentLine.toString());
                    currentLine = new StringBuilder();
                }
                widthLeft = lineWidth;
                lineNumber ++;
            }
            widthLeft -= wordWidth;
            currentLine.append(word);
            currentLine.append(" ");
        }
        lines.add(currentLine.toString());
        return lines;
    }
    
    @Override
    public void render(GameContainer container, Graphics g)
    {
        int imageX = (int)(container.getWidth()/Globals.scaleX/4-width/2);
        int imageY = (int)(container.getHeight()/Globals.scaleY/2 - (int)(height*1.25));
        if( messageBox == null)
            messageBox = new Rectangle(imageX, imageY, width, height);
    
        g.setColor(Color.gray);
        g.fill(messageBox);
        
        
        textGraphics.clear();
        textGraphics.setColor(Color.black);
        textGraphics.setFont(textFont);
        
        int yy = 0;
        for (String line : text.split("\n"))
        {
            textGraphics.drawString(line, paddingX, yy);
            yy += textFont.getHeight(line) + paddingY;
        }
        
        if(name != null)
        {
        	int nameWidth = nameFont.getWidth(name);
        	int nameHeight = nameFont.getHeight(name);
        	int nameX = imageX;
        	int nameY = imageY-nameHeight;
        	Rectangle nameBox = new Rectangle(nameX, nameY, nameWidth+paddingX*2, nameHeight);
        	g.fill(nameBox);
        	
	        nameGraphics.setColor(Color.black);
	        nameGraphics.setFont(nameFont);	        
	        nameGraphics.drawString(name, paddingX, 0);
	        
	        g.drawImage(nameImage, nameX, nameY);
        }
        
        g.drawImage(textImage, imageX, imageY);
       
        
    }

}
